package edu.ccut.saturn.studio.wfg.wfgmodel;

import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.Document;
import org.w3c.dom.Node;

import edu.ccut.saturn.studio.core.util.xml.SaturnW3CUtil;
import edu.ccut.saturn.studio.wfg.model.Shape;
import edu.ccut.saturn.studio.wfg.utils.RunTime;

public class FormDataAttribute implements SaturnAttribute{
	
	private String id = "-1";
	
	private String key = "";
	
	private String name = "";
	
	private String type = "";
	
	private String accessType = "";
	
	private String required = "";
	
	private List<SaturnAttribute> attributes = new ArrayList<SaturnAttribute>();
	
	public FormDataAttribute(FormDataAttribute formDataAttribute){
		this.id = formDataAttribute.id;
		this.key = formDataAttribute.key;
		this.name = formDataAttribute.name;
		this.type = formDataAttribute.type;
		this.accessType = formDataAttribute.accessType;
		this.required = formDataAttribute.required;
		this.attributes = formDataAttribute.attributes;
	}
	
	public FormDataAttribute(){
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getAccessType() {
		return accessType;
	}

	public void setAccessType(String accessType) {
		this.accessType = accessType;
	}

	public String getRequired() {
		return required;
	}

	public void setRequired(String required) {
		this.required = required;
	}

	public Node creatNode(Document document, String NodeName) {
		Node dataNode = document.createElement(NodeName);
		SaturnW3CUtil.createAttribute(dataNode, SaturnName.KEY_NODE_NAME_ID, RunTime.getValue(this, SaturnName.METHOD_GETID));
		SaturnW3CUtil.createAttribute(dataNode, SaturnName.KEY_NODE_NAME_KEY,RunTime.getValue(this, SaturnName.METHOD_GETKEY));
		SaturnW3CUtil.createAttribute(dataNode, Shape.KEY_NODE_NAME,RunTime.getValue(this, SaturnName.METHOD_GETNAME));
		SaturnW3CUtil.createAttribute(dataNode, SaturnName.KEY_NODE_NAME_ACCESSTYPE,RunTime.getValue(this, SaturnName.METHOD_GETACCESSTYPE));
		SaturnW3CUtil.createAttribute(dataNode, SaturnName.KEY_NODE_NAME_REQUIRED,RunTime.getValue(this, SaturnName.METHOD_GETKREQUIRED));
		SaturnW3CUtil.createAttribute(dataNode, SaturnName.XML_TYPE,RunTime.getValue(this, SaturnName.METHOD_GETTYPE));
		createEANodeList(document, dataNode,this.getAttributes(),SaturnName.KEY_NODE_NAME_PROPERTY);
		return dataNode;
	}
	
	protected void createEANodeList(Document document, Node node,
			List<SaturnAttribute> parameters,String nodeName) {
		if (parameters.size()==0 ||parameters == null) {
			return ;
		}
		for (SaturnAttribute parameter : parameters) {
			Node parameterNode = createChildNode(document,parameter);
			node.appendChild(parameterNode);
		}
	}
	
	protected Node createChildNode(Document document,SaturnAttribute attribute) {
		return attribute.creatNode(document, null);
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public List<SaturnAttribute> getAttributes() {
		return attributes;
	}

	public void setAttributes(List<SaturnAttribute> attributes) {
		this.attributes = attributes;
	}
}